﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Wys.DataForm.CSharp
{
    public partial class LogPrint : Wys.Control.Vb.AutoAdjustFrom
    {
        public LogPrint()
        {
            InitializeComponent();
        }

        public DataTable dt = new DataTable();
        private void LogPrint_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("编号");
            dt.Columns.Add("记录时间");
            dt.Columns.Add("具体描述");
            dt.Columns.Add("描述");
            dt.Columns.Add("水平");
            System.Text.RegularExpressions.MatchCollection mcs = null;
            int Rowcount = 0;
            string strContent = "";
            try
            {
                String strPath = System.IO.Path.Combine(this.GetCurrentDirectory(), @"Log\log4netfile.txt");
                if (System.IO.File.Exists(strPath))
                {
                    String strPath2 = System.IO.Path.Combine(this.GetCurrentDirectory(), @"Log\log4netfile_temp.txt");
                    if (System.IO.File.Exists(strPath2))
                    {
                        System.IO.File.Delete(strPath2);
                    }
                    System.IO.File.Copy(strPath, strPath2);
                    strContent = "";
                    char[] content = null;                    
                    int count = 1024 * 502;
                    int index = 0;
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(strPath2, Encoding.Default))
                    {
                        content = new char[1024 * 502];
                        while (sr.Peek() >= 0)
                        {
                            try
                            {
                                sr.Read(content, 0, count);
                                index ++;
                            }
                            catch (ArgumentException ae)
                            {
                                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ae, MethodBase.GetCurrentMethod().GetType());
                            }
                            catch (Exception)
                            {
                                goto End1;
                            }
                           
                            strContent = new string(content);

                            String[] logs = strContent.Split(new String[] { "[Log Time]：" }, StringSplitOptions.RemoveEmptyEntries);
                           
                            String strPattern = @"\[([\s\S]+?)\]([\s\S]+?)Log Level：\[([\s\S]+?)\] Description：([\w\W]+)";

                            foreach (String log in logs)
                            {
                                mcs = System.Text.RegularExpressions.Regex.Matches(log, strPattern);
                                if (mcs.Count > 0)
                                {
                                    if (mcs[0].Groups.Count == 5)
                                    {

                                        if (mcs[0].Groups[3].Value.Trim() != "INFO")
                                        {
                                            String[] strs = mcs[0].Groups[4].Value.Split(new String[] { Environment.NewLine }, StringSplitOptions.
        RemoveEmptyEntries);
                                            if (strs.Length > 1)
                                            {
                                                dt.Rows.Add((Rowcount).ToString(),mcs[0].Groups[1], mcs[0].Groups[4], strs[1], mcs[0].Groups[3]);
                                            }
                                            else
                                            {
                                                dt.Rows.Add((Rowcount).ToString(),mcs[0].Groups[1], mcs[0].Groups[4], mcs[0].Groups[4], mcs[0].Groups[3]);
                                            }

                                        }
                                    }
                                }
                            }
                        }
                    }
                End1:
                    DataView dv = dt.DefaultView;
                    dv.Sort = "记录时间 DESC";
                    DataTable dt1 = dv.ToTable();
                    for (int i = 0; i<dt1.Rows.Count; i++)
                    {
                        dt1.Rows[i]["编号"] = (++Rowcount);
                    }
                    this.myDataView1.AutoGenerateColumns = false;
                    this.myDataView1.DataSource = dt1;
                }
            }           
            catch (Exception me)
            {
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());
            }
           
        }  

        private void myDataView1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (myDataView1.SelectedRows.Count == 0)
                {
                    return;
                }
                this.txtContent.Text = myDataView1.SelectedRows[0].Cells["具体描述"].Value.ToString();
                this.txtTime.Text = myDataView1.SelectedRows[0].Cells["记录时间"].Value.ToString();
            }
            catch (Exception me)
            {                
               UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());
            }

                    
        }

        private void 清空日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                log4net.LogManager.Shutdown();
                String strPath = System.IO.Path.Combine(this.GetCurrentDirectory(), @"Log\log4netfile.txt");
                if (System.IO.File.Exists(strPath))
                {
                    System.IO.FileStream fs = System.IO.File.Open(strPath, System.IO.FileMode.OpenOrCreate);
                    fs.SetLength(0);
                    fs.Close();
                }
                //myDataView1.Rows.Clear();
                myDataView1.DataSource = null;
                dt = null;
                txtContent.Text = "";
                ShowMessageBox("清除成功");
                log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("[Log Time]：[%d] Thread ID:[%t] Log Level：[%-5p] Description：%m%n");
                log4net.Appender.FileAppender appender = new log4net.Appender.FileAppender(layout, strPath, true);
                log4net.Config.BasicConfigurator.Configure(appender);               

                log4net.Config.XmlConfigurator.Configure();
            }
            catch (Exception me)
            {                
               UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());
            }
           
        }
    }
}
